import {ChangeDetectorRef, Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams} from 'ionic-angular';
import {LoadingService} from "../../../services/common/loading.service";
import * as moment from "moment";
import {TeamAddStaffProvider} from "../../../providers/my-team/team-add-staff/team-add-staff.provider";
import {AdpActionSheetController} from '../../../components/common/adp-action-sheet/adp-action-sheet-controller';

@IonicPage()
@Component({
    selector: 'page-team-add-staff-detail',
    templateUrl: 'team-add-staff-detail.html',
    providers: [
        TeamAddStaffProvider,
        LoadingService
    ]
})

export class TeamAddStaffDetailPage {

    constructor(private navCtrl: NavController,
                private teamAddStaffProvider: TeamAddStaffProvider,
                private navParams: NavParams,
                private actionSheetCtrl: AdpActionSheetController,
                public changeRef: ChangeDetectorRef,
                private loadingService: LoadingService) {
    }

    moment = moment;
    /*筛选对象*/
    private filterParams = {
        activityID: '',
        groupID: '',
        isJoin: '',  //0：否，1：是
        sort: 1
    };

    /*列表数据*/
    addStaffDetailList: Array<any> = [];

    /*合计*/
    private totalPreIcreaseNum: number = 0;

    /*营销员总数*/
    private totalAgent: number = 0;

    /*活动名称*/
    public name: string;
    public shortName: string;
    /*活动参与时间*/
    public activityTime: string;
    public groupList: Array<any> = [];

    @ViewChild(Content) content: Content;
    isScrolling = false;
    isArriveBottom = false;

    ionViewDidLoad() {
        this.filterParams.activityID = this.navParams.get('id');
        this.filterParams.groupID = this.navParams.get('groupId');
        this.getAddStaffActivityList();
    }

    /*获取详情*/
    getAddStaffActivityList() {
        this.totalPreIcreaseNum = 0;
        this.loadingService.showLoading();
        this.teamAddStaffProvider.getAddStaffDetail(this.filterParams).subscribe((res) => {
            if (res.length == 0) {
                this.loadingService.dismissLoading();
                return;
            }
            let result = res[0];

            this.name = result.activityName;
            this.shortName = result.shortName;
            this.groupList = [{groupID: '', groupName: '全部组'}].concat(result.groups);

            let addStaffDetailList=result.groupList;
            addStaffDetailList.forEach((item) => {
                item.isJoin = item.isJoin === '1' ? '是' : '否';
                this.totalPreIcreaseNum += parseInt(item.preIcreaseNum || 0);
            });
            if (this.groupList.length === 0) {
                this.groupList = [{groupID: '', groupName: '全部组'}].concat(result.groups);
            }
            this.addStaffDetailList = addStaffDetailList;
            this.totalAgent = addStaffDetailList.length;
            this.activityTime = moment.unix(result.starttime).format('YYYY/MM/DD') + ' - ' + this.moment.unix(result.endtime).format('YYYY/MM/DD');
            this.loadingService.dismissLoading();
        }, e => {
            this.loadingService.dismissLoading();
        });
    }

    showBusinessGroup(ev) {
        let temGroup = [];
        this.groupList.forEach((item) => {
            temGroup.push({
                cssClass: this.filterParams.groupID == item.groupID ? 'action-sheet-selected' : '',
                text: item.groupName,
                handler: () => {
                    this.filterParams.groupID = item.groupID;
                    this.getAddStaffActivityList();
                }
            })
        });
        temGroup.push({
            text: '取消',
            role: 'cancel'
        });
        this.actionSheetCtrl.create({
            title: '业务组',
            cssClass: 'roster-action',
            buttons: temGroup
        }).present();
        ev.stopPropagation();
    }

    showIsActiveAction(ev) {
        this.actionSheetCtrl.create({
            title: '是否参与',
            cssClass: 'roster-action',
            buttons: [
                {
                    cssClass: this.filterParams.isJoin == '' ? 'action-sheet-selected' : '',
                    text: '全部',
                    handler: () => {
                        this.filterParams.isJoin = '';
                        this.getAddStaffActivityList();
                    }
                },
                {
                    cssClass: this.filterParams.isJoin == '1' ? 'action-sheet-selected' : '',
                    text: '是',
                    handler: () => {
                        this.filterParams.isJoin = '1';
                        this.getAddStaffActivityList();
                    }
                },
                {
                    cssClass: this.filterParams.isJoin == '0' ? 'action-sheet-selected' : '',
                    text: '否',
                    handler: () => {
                        this.filterParams.isJoin = '0';
                        this.getAddStaffActivityList();
                    }
                },
                {
                    text: '取消',
                    role: 'cancel'
                },
            ]
        }).present();
        ev.stopPropagation();
    }

    toActivityDetail() {
        this.navCtrl.push('ActivitDetailPage', {
            id: this.filterParams.activityID,
            classify:1
        });
    }

    //滚动相关
    scrollStart() {
        this.isScrolling = true;
    }

    scrollEnd() {
        this.isScrolling = false;
    }

    scrolling() {
        const dimensions = this.content.getContentDimensions();
        const temp1 = dimensions.scrollHeight - dimensions.scrollTop - dimensions.contentHeight < 100;
        if (temp1 !== this.isArriveBottom) {
            this.isArriveBottom = temp1;
            this.changeRef.detectChanges();
        }
    }

    goUp() {
        if (this.isScrolling) {
            setTimeout(() => {
                this.goUp();
            }, 300);
            return;
        }
        this.content.scrollToTop();
    }
}
